import 'package:flutter/material.dart';
import 'dart:convert';
import 'dart:ui';
import 'package:dio/dio.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_yres/tool/constant.dart';
import 'package:flutter_yres/tool/network/http_method.dart';
import 'package:flutter_yres/tool/network/http_url.dart';
import 'package:flutter_yres/tool/AppSaveInfoUtil.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_easyrefresh/material_footer.dart';
import 'package:flutter_easyrefresh/material_header.dart';

EasyRefreshController _controller = new EasyRefreshController();

class BalanceListPage extends StatefulWidget {
  BalanceListPage({Key key}) : super(key: key);

  @override
  _BalanceListPageState createState() => _BalanceListPageState();
}

class _BalanceListPageState extends State<BalanceListPage> {
  String token = "";
  int page = 1;
  List listData = new List();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(
            '余额变动明细',
            style: TextStyle(
                color: black,
                fontSize: ScreenUtil.getInstance().setSp(BIG_FONT_SIZE)),
          ),
          elevation: 0.2,
          backgroundColor: white,
          iconTheme: new IconThemeData(color: black_99),
          brightness: Brightness.light,
        ),
        body: new EasyRefresh(
          //刷新  加载组件
          enableControlFinishRefresh: true,
          enableControlFinishLoad: true,
          controller: _controller,
          header: MaterialHeader(),
          footer: MaterialFooter(),
          // refreshHeader:
          //       RefreshUtils.buildBlackClassicsHeader(context, _headerKey),
          // refreshFooter:
          //       RefreshUtils.buildClassicsFooter(context, _footerKey),
          child: CustomScrollView(slivers: <Widget>[
            SliverFixedExtentList(
                delegate: SliverChildBuilderDelegate(_buildListItem,
                    childCount: listData.length),
                itemExtent: ScreenUtil.getInstance().setSp(24) * 7),
          ]),
          onRefresh: () async {
            page = 1;
            list();
          },
          onLoad: () async {
            page++;
            list();
          },
        ));
  }

  Widget _buildListItem(BuildContext context, int index) {
    return new GestureDetector(
        onTap: () {},
        child: Container(
            color: white,
            margin: EdgeInsets.fromLTRB(
                0, ScreenUtil.getInstance().setHeight(15), 0, 0),
            padding: EdgeInsets.fromLTRB(
                ScreenUtil.getInstance().setWidth(10),
                ScreenUtil.getInstance().setWidth(10),
                ScreenUtil.getInstance().setWidth(10),
                ScreenUtil.getInstance().setWidth(10)),
            child: Column(
              children: <Widget>[
                Row(children: <Widget>[
                  Expanded(
                    child: Text(
                      listData[index]['action_zh'].toString(),
                      style: TextStyle(
                          fontSize: ScreenUtil.getInstance().setSp(24)),
                    ),
                  ),
                  Expanded(
                      child: Container(
                    alignment: Alignment.centerRight,
                    child: Text(
                      listData[index]['action_symbol'].toString() +
                          '¥' +
                          listData[index]['money'].toString(),
                      style: TextStyle(
                          fontSize: ScreenUtil.getInstance().setSp(24)),
                    ),
                  ))
                ], crossAxisAlignment: CrossAxisAlignment.start),
                Container(
                  height: 5,
                ),
                listData[index]['action'].toString() == 'transfer_sub'
                    ?  Container(
                        alignment: Alignment.centerLeft,
                        child:  Text('转给:' + listData[index]['accept'].toString(),style: TextStyle(
                          fontSize: ScreenUtil.getInstance().setSp(24))),
                      )
                    : Container(),
                    listData[index]['action'].toString() == 'transfer_add'
                    ?  Container(
                        alignment: Alignment.centerLeft,
                        child:  Text('来自:' + listData[index]['accept'].toString(),style: TextStyle(
                          fontSize: ScreenUtil.getInstance().setSp(24))),
                      )
                    : Container(),
                Container(
                  height: 5,
                ),
                Row(children: <Widget>[
                  Expanded(
                    child: Text(
                      '变动时间:' + listData[index]['create_time'].toString(),
                      style: TextStyle(
                          fontSize: ScreenUtil.getInstance().setSp(24)),
                    ),
                  ),
                  Expanded(
                      child: Container(
                    alignment: Alignment.centerRight,
                    child: Text(
                      '当前总额:¥' + listData[index]['all_money'].toString(),
                      style: TextStyle(
                          fontSize: ScreenUtil.getInstance().setSp(24)),
                    ),
                  ))
                ], crossAxisAlignment: CrossAxisAlignment.start),
                // Divider(
                //   height: 1,
                // )
              ],
            )));
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getString('token').then((value) {
      token = value;
      list();
    });
  }

  list() async {
    FormData data =
        new FormData.fromMap({'token': token, 'p': page.toString()});
    Response<String> response =
        await HttpUtil().post(Api.GetBalanceRecordUrl, data: data);
    Map userMap = json.decode(response.data);
    print(userMap);
    int code = userMap["code"];
    String msg = userMap["msg"];
    if (code == 1 && mounted) {
      setState(() {
        if (page == 1) {
          listData.clear();
          listData = userMap['data']['list'];
          _controller.finishRefresh();
        } else {
          listData.addAll(userMap['data']['list']);
          _controller.finishLoad();
        }
      });
    }
  }
}
